set(SPANATTN_KERNEL ${CMAKE_PROJECT_NAME}_kernel)
set(SPANATTN_LIB ${CMAKE_PROJECT_NAME}_lib)

# kernel
set(SPANATTN_KERNEL_INC_DIR
    ${CMAKE_CURRENT_SOURCE_DIR}
)
file(GLOB_RECURSE SPANATTN_KERNEL_SRC
    ${CMAKE_CURRENT_SOURCE_DIR}/attn/*.cu
    ${CMAKE_CURRENT_SOURCE_DIR}/cache_quant/*.cu
    ${CMAKE_CURRENT_SOURCE_DIR}/utils/*.cu
)

add_library(${SPANATTN_KERNEL}_objs OBJECT ${SPANATTN_KERNEL_SRC})
target_include_directories(${SPANATTN_KERNEL}_objs
    PUBLIC ${SPANATTN_INTERFACE_DIR}
    PRIVATE ${SPANATTN_KERNEL_INC_DIR}
)
target_compile_definitions(${SPANATTN_KERNEL}_objs
    PUBLIC ${SPANATTN_DEFINITION}
)
target_link_libraries(${SPANATTN_KERNEL}_objs
    PRIVATE ${CUTLASS_LIBRARY}
)

# lib
set(SPANATTN_INC_DIR
    ${CMAKE_CURRENT_SOURCE_DIR}
)
file(GLOB_RECURSE SPANATTN_SRC
    ${CMAKE_CURRENT_SOURCE_DIR}/attn/*.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/common/*.cpp
)

add_library(${SPANATTN_LIB}_objs OBJECT ${SPANATTN_SRC})
target_include_directories(${SPANATTN_LIB}_objs
    PUBLIC ${SPANATTN_INTERFACE_DIR}
    PRIVATE ${SPANATTN_INC_DIR}
)
target_compile_definitions(${SPANATTN_LIB}_objs
    PUBLIC ${SPANATTN_DEFINITION}
)
target_link_libraries(${SPANATTN_LIB}_objs
    PRIVATE ${CUTLASS_LIBRARY}
    PRIVATE ${SPANATTN_CUDART_LIBRARY}
)

# shared lib
add_library(${SPANATTN_LIB_SHARED} SHARED
    $<TARGET_OBJECTS:${SPANATTN_KERNEL}_objs>
    $<TARGET_OBJECTS:${SPANATTN_LIB}_objs>
)
target_include_directories(${SPANATTN_LIB_SHARED}
    INTERFACE
        $<BUILD_INTERFACE:${SPANATTN_INTERFACE_DIR}>
        $<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>
)

# static lib
add_library(${SPANATTN_LIB_STATIC} STATIC
    $<TARGET_OBJECTS:${SPANATTN_KERNEL}_objs>
    $<TARGET_OBJECTS:${SPANATTN_LIB}_objs>
)
target_include_directories(${SPANATTN_LIB_STATIC}
    INTERFACE
        $<BUILD_INTERFACE:${SPANATTN_INTERFACE_DIR}>
        $<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>
)

# lib output dir
set_target_properties(${SPANATTN_LIB_SHARED} ${SPANATTN_LIB_STATIC}
    PROPERTIES
        ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
        LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
        OUTPUT_NAME ${CMAKE_PROJECT_NAME}
)
